﻿#region Disclaimer/Info
///////////////////////////////////////////////////////////////////////////////////////////////////
// MetaCollection
// 
// MetaCollection is a system to provide a standard structure, permissions and metadata service
// for content organization.
//
// This project is hosted at Google Code at http://code.google.com/p/metacollection/
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;

namespace MetaCollection.Framework.Model
{
	/// <summary>
	/// An ItemClaim is a claim assigned to a resource that is checked against a principal with
	/// the goal of assigning the Rights contained in the claim to the principal for the Item.
	/// </summary>
	/// <remarks>
	/// The following pseudo code describes the permission process for an Item with ItemClaims.
	///    If a Principal has Claims and one of the claims matches <see cref="T:IItemClaim.ClaimType"/> and
	///    this claim's value matches <see cref="T:IItemClaim.ClaimValue"/> then the Principal is assigned
	///    the <see cref="T:IItemClaim.Rights"/> to the <see cref="T:IItemInstance"/> that the <see cref="T:IItemClaim"/>
	///    belongs to.
	/// </remarks>
	public interface IItemClaim
	{
		/// <summary>
		/// Unique identifer for this claim.
		/// </summary>
		Guid Id { get; set; }
		/// <summary>
		/// Type of this claim.
		/// </summary>
		/// <example>http://schemas.microsoft.com/ws/2008/06/identity/claims/role</example>
		string ClaimType { get; set; }
		/// <summary>
		/// Value that must be present when the ClaimType is evaluated against a principal.
		/// </summary>
		string ClaimValue { get; set; }
		/// <summary>
		/// A set of AccessFlags (rights) that are granted to a Principal when this claim is
		/// satisfied.
		/// </summary>
		AccessRights Rights { get; set; }
	}
	/// <summary>
	/// A structure representing an ItemClaim
	/// </summary>
	[Serializable()]
	public struct ItemClaim : IItemClaim
	{
		/// <summary>
		/// Unique identifer for this claim.
		/// </summary>
		public Guid Id { get; set; }
		/// <summary>
		/// Type of this claim.
		/// </summary>
		/// <example>http://schemas.microsoft.com/ws/2008/06/identity/claims/role</example>
		public string ClaimType { get; set; }
		/// <summary>
		/// Value that must be present when the ClaimType is evaluated against a principal.
		/// </summary>
		public string ClaimValue { get; set; }
		/// <summary>
		/// A set of AccessFlags (rights) that are granted to a Principal when this claim is
		/// satisfied.
		/// </summary>
		public AccessRights Rights { get; set; }
	}

	public static partial class Extensions
	{
		/// <summary>
		/// Converts an enumerable set of IItemClaim to a simple structure implementation of the interface
		/// </summary>
		/// <param name="claims"></param>
		/// <returns>An enumerable collection of Item Claim structures.</returns>
		public static IEnumerable<ItemClaim> ToStruct(this IEnumerable<IItemClaim> claims)
		{
			if (claims == null)
				yield break;

			foreach (var claim in claims)
			{
				ItemClaim itemClaim = new ItemClaim();
				itemClaim.ClaimType = claim.ClaimType;
				itemClaim.ClaimValue = claim.ClaimValue;
				itemClaim.Id = claim.Id;
				itemClaim.Rights = claim.Rights;
				yield return itemClaim;
			}
			yield break;
		}
	}
}
